package com.swj.common.core.utils.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * localdatetime 工具类
 *
 * @author 10834
 */
public class LocalDateTimeUtil {

    public static final String YYYY = "yyyy";

    public static final String YYYY_MM = "yyyy-MM";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYY_MM_DD_HH_MM_SS_WITHWORD = "yyyy年MM月dd日 HH时mm分ss秒";

    public LocalDateTime getLocalDateTime(){
        return LocalDateTime.now();
    }

    public String getLocalDateTimeString(){
        return LocalDateTime.now().toString();
    }

    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * Str ==> LocalDateTime
     */
    public static LocalDateTime strToLocalDateTime(String time) {
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
    }

    /**
     * Str ==> LocalDateTime
     */
    public static LocalDateTime strToLocalDateTime(String time,String pattern) {
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(pattern));
    }

}
